package me.eleme.anubis.openapi.demo;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import me.eleme.anubis.openapi.api.entity.order.*;
import me.eleme.anubis.openapi.api.entity.shop.*;
import me.eleme.anubis.openapi.api.exception.ServiceException;
import me.eleme.anubis.openapi.api.service.OrderService;
import me.eleme.anubis.openapi.api.service.ShopService;
import me.eleme.anubis.openapi.config.Config;
import me.eleme.anubis.openapi.config.oauth.response.Token;

import java.util.ArrayList;
import java.util.List;

/**
 * sdk使用示例代码
 * @author liuzhiyong
 */
@Slf4j
public class HttpClientDemo {

    /**
     * 设置是否沙箱环境
     */
    private static final boolean IS_SANDBOX = true;
    /**
     * 设置APP_ID
     */
    private static final String APP_ID = "app id";
    private static final String MERCHANT_ID = "merchant id";
    /**
     * 设置APP SECRET_KEY
     */
    private static final String SECRET_KEY = "secret key";

    private final static Config CONFIG;
    private final static Token TOKEN;

    static {
        // 初始化全局配置工具
        CONFIG = new Config(IS_SANDBOX, APP_ID, SECRET_KEY);
        TOKEN = new Token();
        TOKEN.setAccessToken("");
        TOKEN.setAppId(APP_ID);
        TOKEN.setMerchantId(MERCHANT_ID);
    }

    public static void main(String[] args) {
        HttpClientDemo demo = new HttpClientDemo();
        demo.addTipTest();
    }


    // --------------------- 店铺 ---------------------

    private void uploadImgTest(){
        ShopService shopService = new ShopService(CONFIG,TOKEN);
        UploadImgReq uploadImgReq = new UploadImgReq();
        uploadImgReq.setFileType("jpg");
        String base64Str = "";
        uploadImgReq.setFileBinary(base64Str);
        try {
            UploadImgRes uploadImgRes = shopService.uploadImg(uploadImgReq);
            log.info("uploadImgRes:{}", JSON.toJSONString(uploadImgRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }

    /**
     * 单个门店查询
     */
    private void chainstoreQueryTest() {
        ShopService shopService = new ShopService(CONFIG,TOKEN);
        QueryChainStoreReq queryChainStoreReq = new QueryChainStoreReq();
        queryChainStoreReq.setMerchantId(MERCHANT_ID);
        queryChainStoreReq.setChainStoreId("100000");
        try {
            ShopInfo shop = shopService.getShop(queryChainStoreReq);
            log.info("shop:{}", JSON.toJSONString(shop));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }

    /**
     * 门店列表查询
     */
    private void chainstoreQueryListTest() {
        ShopService shopService = new ShopService(CONFIG,TOKEN);
        ChainstoreQueryListReq chainstoreQueryListReq = new ChainstoreQueryListReq();
        chainstoreQueryListReq.setMerchantId(MERCHANT_ID);
        chainstoreQueryListReq.setPageNo("1");
        chainstoreQueryListReq.setPageSize("10");

        try {
            ChainstoreQueryListRes chainstoreQueryList = shopService.chainstoreQueryList(chainstoreQueryListReq);
            log.info("chainstoreQueryList:{}", JSON.toJSONString(chainstoreQueryList));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 门店创建示例
     */
    private void chainstoreCreateTest() {
        ShopService shopService = new ShopService(CONFIG,TOKEN);
        ChainstoreCreateReq chainstoreCreateReq = new ChainstoreCreateReq();
        chainstoreCreateReq.setHeadShopName("门店构建名字");
        chainstoreCreateReq.setBranchShopName("大望路店xx");
        chainstoreCreateReq.setChainStoreType(1);
        chainstoreCreateReq.setContactPhone("17700000000");
        chainstoreCreateReq.setAddress("北京市朝阳区西大望路1号");
        chainstoreCreateReq.setLongitude("118.1342");
        chainstoreCreateReq.setLatitude("40.235");
        chainstoreCreateReq.setPositionSource(3);
        chainstoreCreateReq.setOutShopCode("testKFC003444");
        chainstoreCreateReq.setCategoryId("12");
        chainstoreCreateReq.setOwnerName("刘xx");
        chainstoreCreateReq.setOwnerIdNum("410184198801011113");
        chainstoreCreateReq.setHandheldLicencePicHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreCreateReq.setOwnerIdPicBackHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreCreateReq.setOwnerIdPicFrontHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreCreateReq.setCreditCode("910X11103345");
        chainstoreCreateReq.setBusinessLicencePicHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreCreateReq.setFoodLicensePicHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreCreateReq.setSettlementModel("2");
        chainstoreCreateReq.setSettlementAccountId("102099");
        try {
            ChainstoreCreateRes chainstoreCreateRes = shopService.createChainstore(chainstoreCreateReq);
            log.info("chainstoreCreateRes:{}", JSON.toJSONString(chainstoreCreateRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }

    }


    /**
     * 门店更新示例
     */
    private void chainstoreUpdateTest() {
        ShopService shopService = new ShopService(CONFIG,TOKEN);
        ChainstoreUpdateReq chainstoreUpdateReq = new ChainstoreUpdateReq();
        chainstoreUpdateReq.setChainStoreId("204409641");
        chainstoreUpdateReq.setHeadShopName("门店构建名字更改");
        chainstoreUpdateReq.setBranchShopName("大望路AAAA店");
//        chainstoreUpdateReq.setChainStoreType(1);
//        chainstoreUpdateReq.setContactPhone("17701260288");
//        chainstoreUpdateReq.setAddress("北京市朝阳区西大望路1号");
        chainstoreUpdateReq.setLongitude("118.1342");
        chainstoreUpdateReq.setLatitude("40.235");
        chainstoreUpdateReq.setPositionSource(3);
        chainstoreUpdateReq.setOutShopCode("testKFC003444");
        chainstoreUpdateReq.setCategoryId("12");
        chainstoreUpdateReq.setOwnerName("刘xx");
        chainstoreUpdateReq.setOwnerIdNum("410184198801011113");
        chainstoreUpdateReq.setHandheldLicencePicHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreUpdateReq.setOwnerIdPicBackHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreUpdateReq.setOwnerIdPicFrontHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreUpdateReq.setCreditCode("910X11103345");
        chainstoreUpdateReq.setBusinessLicencePicHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
        chainstoreUpdateReq.setFoodLicensePicHash("d7c64022f6458f9aa76968e01f5686c5jpeg");
//
        try {
            ChainstoreUpdateRes chainstoreUpdateRes = shopService.updateChainstore(chainstoreUpdateReq);
            log.info("chainstoreUpdateRes:{}", JSON.toJSONString(chainstoreUpdateRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }


    }

    // --------------------- 订单 ---------------------

    /**
     * 查询配送范围
     */
    private void chainstoreRangeTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        ChainstoreRangeReq chainstoreRangeReq = new ChainstoreRangeReq();
        chainstoreRangeReq.setChainStoreId(203040011);
        try {
            ChainstoreRangeRes chainstoreRangeRes = orderService.chainstoreRange(chainstoreRangeReq);
            log.info("chainstoreRangeRes:{}", JSON.toJSONString(chainstoreRangeRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 预下单
     */
    private void preCreateOrderTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        PreCreateOrderReq preCreateOrderReq = new PreCreateOrderReq();
        preCreateOrderReq.setChainStoreId(203040091L);
        preCreateOrderReq.setPartnerOrderCode("JSD4101231333235567");
        preCreateOrderReq.setBaseGoodsId(3000);
        preCreateOrderReq.setOrderSource(4);
        preCreateOrderReq.setOrderType(1);
        preCreateOrderReq.setPositionSource(3);
        preCreateOrderReq.setReceiverAddress("北京市昌平区中东路0000");
        preCreateOrderReq.setReceiverLatitude(40.039115);
        preCreateOrderReq.setReceiverLongitude(116.39685489017384);
        preCreateOrderReq.setGoodsTotalAmountCent(1000L);
        preCreateOrderReq.setGoodsActualAmountCent(1000L);
        preCreateOrderReq.setRequireReceiveTime(System.currentTimeMillis() + 1000*60*60);
        preCreateOrderReq.setGoodsWeight(0.1);
        preCreateOrderReq.setGoodsCount(1);
        OrderItemOpenapiDTO orderItemOpenapiDTO = new OrderItemOpenapiDTO();
        orderItemOpenapiDTO.setItemName("KFC汉堡一打");
        orderItemOpenapiDTO.setItemQuantity(2);
        orderItemOpenapiDTO.setItemActualAmountCent(500L);
        orderItemOpenapiDTO.setItemAmountCent(500L);
        List<OrderItemOpenapiDTO> goodsItemList = new ArrayList<>();
        goodsItemList.add(orderItemOpenapiDTO);
        preCreateOrderReq.setGoodsItemList(goodsItemList);
        log.info("preCreateOrderReq:{}",JSON.toJSONString(preCreateOrderReq));
        try {
            PreCreateOrderRes preCreateOrderRes = orderService.preCreateOrder(preCreateOrderReq);
            log.info("preCreateOrderRes:{}", JSON.toJSONString(preCreateOrderRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }

    /**
     * 查询余额
     */
    private void getAmountTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            GetAmountRes amount = orderService.getAmount();
            log.info("amount:{}", JSON.toJSONString(amount));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 查询订单详情
     */
    private void getOrderDetailTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            GetOrderDetailReq orderDetailReq = new GetOrderDetailReq();
//            orderDetailReq.setOrderId("150000001061018385");
            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            GetOrderDetailRes orderDetail = orderService.getOrderDetail(orderDetailReq);
            log.info("orderDetail:{}", JSON.toJSONString(orderDetail));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }





    /**
     * 预下单
     */
    private void createOrderTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        CreateOrderReq createOrderReq = new CreateOrderReq();
        createOrderReq.setChainStoreId("203040091");
        createOrderReq.setPartnerOrderCode("JSD4101231333235567");
        createOrderReq.setBaseGoodsId("3000");
        createOrderReq.setOrderSource("4");
        createOrderReq.setOrderType(1);
        createOrderReq.setPositionSource(3);
        createOrderReq.setReceiverAddress("北京市昌平区中东路00000号");
        createOrderReq.setReceiverLatitude(40.039115);
        createOrderReq.setReceiverLongitude(116.307892);
        createOrderReq.setGoodsTotalAmountCent(1000L);
        createOrderReq.setGoodsActualAmountCent(1000L);
        createOrderReq.setRequireReceiveTime(System.currentTimeMillis() + 1000*60*60);
        createOrderReq.setGoodsWeight(0.1);
        createOrderReq.setGoodsCount(1);
        createOrderReq.setReceiverName("张三");
        createOrderReq.setReceiverPrimaryPhone("13123454567");
        OrderItemOpenapiDTO orderItemOpenapiDTO = new OrderItemOpenapiDTO();
        orderItemOpenapiDTO.setItemName("KFC汉堡一打");
        orderItemOpenapiDTO.setItemQuantity(2);
        orderItemOpenapiDTO.setItemActualAmountCent(500L);
        orderItemOpenapiDTO.setItemAmountCent(500L);
        List<OrderItemOpenapiDTO> goodsItemList = new ArrayList<>();
        goodsItemList.add(orderItemOpenapiDTO);
        createOrderReq.setGoodsItemList(goodsItemList);
        log.info("createOrderReq:{}",JSON.toJSONString(createOrderReq));
        try {
            CreateOrderRes order = orderService.createOrder(createOrderReq);
            log.info("order:{}", JSON.toJSONString(order));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }

    /**
     * 取消预下单
     */
    private void preCancelOrderTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            PreCancelOrderReq orderDetailReq = new PreCancelOrderReq();
//            orderDetailReq.setOrderId("150000001061018385");
            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            PreCancelOrderRes preCancelOrderRes = orderService.preCancelOrder(orderDetailReq);
            log.info("preCancelOrderRes:{}", JSON.toJSONString(preCancelOrderRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 取消正式下单
     */
    private void cancelOrderTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            CancelOrderReq orderDetailReq = new CancelOrderReq();
//            orderDetailReq.setOrderId("150000001061018385");
            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            orderDetailReq.setOrderCancelRole(2);
            orderDetailReq.setOrderCancelCode(32);
            CancelOrderRes cancelOrderRes = orderService.cancelOrder(orderDetailReq);
            log.info("cancelOrderRes:{}", JSON.toJSONString(cancelOrderRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 获取取消原因列表
     */
    private void getCancelReasonListTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            CancelReasonListReq orderDetailReq = new CancelReasonListReq();
            orderDetailReq.setOrderId("150000001061018385");
//            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            CancelReasonListRes cancelReasonList = orderService.getCancelReasonList(orderDetailReq);
            log.info("cancelReasonList:{}", JSON.toJSONString(cancelReasonList));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 投诉订单
     */
    private void complaintOrderTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            ComplaintOrderReq complaintOrderReq = new ComplaintOrderReq();
            complaintOrderReq.setOrderId("150000001061018385");
            complaintOrderReq.setOrderComplaintCode(130);
//            complaintOrderReq.setOrderComplaintDesc("其他");
//            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            ComplaintOrderRes complaintOrderRes = orderService.complaintOrder(complaintOrderReq);
            log.info("complaintOrderRes:{}", JSON.toJSONString(complaintOrderRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }

    /**
     * 索赔订单
     */
    private void claimOrderTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            ClaimOrderReq claimOrderReq = new ClaimOrderReq();
            claimOrderReq.setOrderId("150000001061018385");
            claimOrderReq.setOrderClaimCode(150);
            claimOrderReq.setOrderClaimPriceCent(1000L);
//            complaintOrderReq.setOrderComplaintDesc("其他");
//            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            ClaimOrderRes claimOrderRes = orderService.claimOrder(claimOrderReq);
            log.info("claimOrderRes:{}", JSON.toJSONString(claimOrderRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }


    /**
     * 追加消费
     */
    private void addTipTest() {
        OrderService orderService = new OrderService(CONFIG,TOKEN);
        try {
            AddTipReq addTipReq = new AddTipReq();
            addTipReq.setOrderId("150000001104501521");
            addTipReq.setAddTipAmountCent(500L);
            addTipReq.setThirdIndexId(1L);
//            complaintOrderReq.setOrderComplaintDesc("其他");
//            orderDetailReq.setPartnerOrderCode("232232999uuF21");
            AddTipRes addTipRes = orderService.addTip(addTipReq);
            log.info("addTipRes:{}", JSON.toJSONString(addTipRes));
        } catch (ServiceException e) {
            log.info("ServiceException，e:{}",e);
        }
    }

}
